
var PermissionGroup=function () {

    bootbox.setDefaults({
        'locale':'zh_CN'
    });

    $.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };

    return{

        initPermission:function(){
            var setting = {
                view: {
                    dblClickExpand: false
                },
                check: {
                    enable:true
                }
            };

            setting.check.chkboxType = { "Y" : "s", "N" : "s" };

            $.ajax({
                url: "/api/permission/findAllSysPermission",
                dataType: "json",
                success: function (data) {
                    if (data.code == '401') {
                        bootbox.alert(data.message);
                    } else {
                        $.fn.zTree.init($("#permissionTree"), setting, data.data);

                    }
                }
            });
        },
        initMenu:function(){
            var setting = {
                view: {
                    dblClickExpand: false
                },
                check: {
                    enable:true
                }
            };

            setting.check.chkboxType = { "Y" : "s", "N" : "s" };

            $.ajax({
                url: "/api/menu/findAll",
                dataType: "json",
                success: function (data) {
                    if (data.code == '401') {
                        bootbox.alert(data.message);
                    } else {
                        $.fn.zTree.init($("#menuTree"), setting, data.data);

                    }
                }
            });
        },
        showAddModal:function(){
            $('#addGroup').modal({
                show:true
            });
            $('#addGroup h4').text("添加权限组");
        },
        getGroup:function (id) {
            $.ajax({
                url: "/api/group/getGroup",
                data:{id:id},
                type:'GET',
                dataType: "json",
                success: function (data) {
                    if(data.code =='401'){
                        bootbox.alert(data.message);
                    }else if(data.code =='200'){
                        Form.loadData(data.data);
                        $('#roles').multiSelect('select',data.data.roles.toString());
                        $('#addGroup').modal({
                            show:true
                        });
                        $('#addGroup h4').text("编辑权限组");
                    }
                }
            });
        },
        delete:function (id) {
            bootbox.confirm('确定要删除该权限吗?',function(btn){
                if(btn){
                    $.ajax({
                        url: "/api/group/delete",
                        data:{id:id},
                        type:'GET',
                        dataType: "json",
                        success: function (data) {
                            if(data.code =='401'){
                                bootbox.alert(data.message);
                            }else if(data.code =='200' && data.data){
                                $('#groupTable').dataTable().fnDraw();
                            }else {
                                bootbox.alert(data.message);
                            }
                        }
                    });
                }
            });
        },
        save:function(){
            var data = $('#groupForm').serializeObject();
            $.ajax({
                url: "/api/group/save",
                data:JSON.stringify(data),
                type:'POST',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    if(data.code =='401'){
                        bootbox.alert(data.message);
                    }else{
                        if(data.data){
                            $('#addGroup').modal("hide");
                            $('#groupTable').dataTable().fnDraw();
                            PermissionGroup.formReset();
                        }else{
                            bootbox.alert(data.message)
                        }
                    }
                }
            });
        },
        savePermission:function(){
            var menuTree = $.fn.zTree.getZTreeObj("menuTree");
            var permissionTree = $.fn.zTree.getZTreeObj("permissionTree");
            var menuNodes=menuTree.getCheckedNodes(true);
            var permissionNodes=permissionTree.getCheckedNodes(true);
            var groupTable=$('#groupTable').DataTable();
            var data=groupTable.rows('.selected').data()[0];
            if(data){
                if(menuNodes.length==0 && permissionNodes.length ==0){
                    bootbox.alert("没有做任何操作,请选择菜单！")
                }else {
                    var checkMenuNodes=[];
                    var checkPermissionNodes=[];
                    for (var i = 0; i <permissionNodes.length ; i++) {
                        if(permissionNodes[i].type==1){
                            checkPermissionNodes.push(permissionNodes[i].id);
                        }
                    }

                    for (var i = 0; i <menuNodes.length ; i++) {
                        checkMenuNodes.push(menuNodes[i].id);
                    }

                    var data={
                        groupId:data.id,
                        menuNodes:checkMenuNodes,
                        permissionNodes:checkPermissionNodes
                    };

                    $.ajax({
                        url: "/api/permissionGroup/save",
                        data:JSON.stringify(data),
                        type:'POST',
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            if(data.code =='401'){
                                bootbox.alert(data.message);
                            }else{
                                if(data.code =='200' && data.data){
                                    bootbox.alert(data.message)
                                }else{
                                    bootbox.alert(data.message)
                                }
                            }
                        }
                    });

                }
            }else {
                bootbox.alert("请选择一个权限！");
            }
        },
        initRole:function(){
            $.ajax({
                url: "/api/role/findAllRole",
                type:'GET',
                dataType: "json",
                success: function (data) {
                    if(data.code =='401'){
                        bootbox.alert(data.message);
                    }else{
                        if(data.code =='200'){

                            $('#roles').multiSelect({
                                selectableHeader: "<div class='custom-header'>角色列表</div>",
                                selectionHeader: "<div class='custom-header'>已选角色</div>"
                            });

                            $('#roles').multiSelect('addOption',data.data);
                        }else{
                            bootbox.alert(data.message)
                        }
                    }
                }
            });
        },
        formReset:function(){
            $('#groupForm')[0].reset();
            $('#id').val('');
            $(':input','#roleForm').removeAttr('checked');
            $('#roleIds').multiSelect('refresh');
        }
    }
}();